package cn.cdeden.common.oss.util;


import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName : KnowCore01
 * @Package : cn.cdeden.common.oss.util
 * @ClassName : FileUtil
 * @Author : Yu.
 * @Description : // 文件处理工具类
 * @Date : 2024/12/4 16:41
 * @Version : 1.0
 */
public class FileProcessingUtil extends FileUtil {

    public static final String IMAGE = "图片";
    public static final String TXT = "文档";
    public static final String MUSIC = "音乐";
    public static final String VIDEO = "视频";
    public static final String OTHER = "其他";


    /**
     * 定义TB的计算常量
     */
//    private static final long TB = 1024 * 1024 * 1024 * 1024; // final  无法计算
    private static final long TB = 1099511627776L;
    /**
     * 定义GB的计算常量
     */
    private static final long GB = 1073741824L;
//    private static final long GB = 1024 * 1024 * 1024;
    /**
     * 定义MB的计算常量
     */
//    private static final long MB = 1024 * 1024;
    private static final long MB = 1048576L;
    /**
     * 定义KB的计算常量
     */
    private static final long KB = 1024L;

    /**
     * 格式化小数
     */
    private static final DecimalFormat DF = new DecimalFormat("0.00");

    public static final long FILE_SIZE ;

    static {
        FILE_SIZE = 1048576L * 800; // 500M文件上传大小为500M
    }



//    private static final JSONObject fileJson = JSONObject.parseObject("{\"state\":\"true\",\"data\":[{\"name\":\"Office\",\"state\":\"true\",\"data\":[\"doc\",\"docx\",\"xls\",\"xlsx\",\"ppt\",\"pptx\"]},{\"name\":\"Flash\",\"state\":\"true\",\"data\":[\"swf\"]},{\"name\":\"PDF\",\"state\":\"true\",\"data\":[\"pdf\"]},{\"name\":\"HTML\",\"state\":\"true\",\"data\":[\"html\",\"js\",\"css\",\"htm\"]},{\"name\":\"audio\",\"state\":\"true\",\"data\":[\"mp3\",\"wma\"]},{\"name\":\"video\",\"state\":\"true\",\"data\":[\"mp4\",\"flv\",\"f4v\",\"mov\"]},{\"name\":\"image\",\"state\":\"true\",\"data\":[\"jpg\",\"jpeg\",\"png\",\"bmp\",\"ico\",\"gif\"]},{\"name\":\"text\",\"state\":\"true\",\"data\":[\"sql\",\"txt\",\"js\",\"java\",\"conf\",\"ini\"]},{\"name\":\"CAD\",\"state\":\"true\",\"data\":[\"dwf\"]},{\"name\":\"zip\",\"state\":\"true\",\"data\":[\"zip\",\"rar\"]}]}");

    private static final JSONObject fileJson = JSONObject.parseObject("{\"state\":\"false\",\"data\":[{\"name\":\"Office\",\"state\":\"true\",\"data\":[\"doc\",\"docx\",\"xls\",\"xlsx\",\"ppt\",\"pptx\"]},{\"name\":\"Flash\",\"state\":\"true\",\"data\":[\"swf\"]},{\"name\":\"PDF\",\"state\":\"true\",\"data\":[\"pdf\"]},{\"name\":\"HTML\",\"state\":\"true\",\"data\":[\"html\",\"js\",\"css\",\"htm\"]},{\"name\":\"audio\",\"state\":\"true\",\"data\":[\"mp3\",\"wma\"]},{\"name\":\"video\",\"state\":\"true\",\"data\":[\"mp4\",\"flv\",\"f4v\",\"mov\"]},{\"name\":\"image\",\"state\":\"true\",\"data\":[\"jpg\",\"jpeg\",\"png\",\"bmp\",\"ico\",\"gif\"]},{\"name\":\"text\",\"state\":\"true\",\"data\":[\"sql\",\"txt\",\"js\",\"java\",\"conf\",\"ini\"]},{\"name\":\"CAD\",\"state\":\"true\",\"data\":[\"dwf\"]},{\"name\":\"zip\",\"state\":\"true\",\"data\":[\"zip\",\"rar\"]}]}");


    /**
     * @author Yu.
     * @Description // 比较大小
     * @Date 2024/12/4 17:24
     * @param size01
     * @param size02
     * @return boolean
     **/
    public static boolean sizeComparison(long size01,long size02) {
        return size01 < size02;
    }


    /**
     * @author Yu.
     * @Description // 获取系统可以上传的文件
     * @Date 2024/12/4 17:23
     * @param fileType
     * @return boolean
     **/
    public static boolean getFileSuffixBy(String fileType) {
        boolean b = true;
        if (fileJson.containsKey("state")) { //判断整个模块是否需要验证文件
            Boolean objectBoolean = fileJson.getBoolean("state");
            if (!objectBoolean) {
                return true;
            }
        }

        List<String> list = new ArrayList<>();
        JSONArray data = fileJson.getJSONArray("data");
        for (int i = 0; i < data.size(); i++) {
            JSONObject object = data.getJSONObject(i);
            Boolean state = object.getBoolean("state");
            if (state) { // 判断这个模块是否加入数据
                JSONArray jsonArray = object.getJSONArray("data");
                List<String> list1 = jsonArray.toJavaList(String.class);
                list.addAll(list1);
            }
        }
        String lowerCase = fileType.toLowerCase();
        b = list.contains(lowerCase);

        return b;
    }



    /**
     * @author Yu.
     * @Description // 字节转换成对应 文件的变量大小
     * @Date 2024/12/4 16:42
     * @param size
     * @return java.lang.String
     **/
    public static String getSizeToUnit(long size) {
        String resultSize;
        if (size == 0) {
            resultSize = size + "B ";
        } else if (size / TB >= 1) {
            //如果当前Byte的值大于等于1GB
            resultSize = DF.format(size / (float) TB) + "TB ";
        } else if (size / GB >= 1) {
            //如果当前Byte的值大于等于1GB
            resultSize = DF.format(size / (float) GB) + "GB ";
        } else if (size / MB >= 1) {
            //如果当前Byte的值大于等于1MB
            resultSize = DF.format(size / (float) MB) + "MB ";
        } else if (size / KB >= 1) {
            //如果当前Byte的值大于等于1KB
            resultSize = DF.format(size / (float) KB) + "KB ";
        } else {
            resultSize = size + "B ";
        }
        return resultSize;
    }
}
